package com.sayice.coursetable.logic.dao;

import android.content.Context;
import android.util.Log;

import com.sayice.coursetable.CourseTableApp;
import com.sayice.coursetable.logic.model.Course;
import com.sayice.coursetable.logic.utils.AesUtils;

/**
 * SharedPreferences 操作
 */
public interface PreferencesDao {
    /**
     * 保存用户信息
     *
     * @param name     用户名
     * @param password 密码
     */
    static void saveUserInfo(String name, String password) {
        CourseTableApp.context.getSharedPreferences("data", Context.MODE_PRIVATE)
                .edit()
                .putString("username", name)
                .putString("password", AesUtils.encryptAES(password, AesUtils.AES_KEY))
                .apply();
    }

    /**
     * 保存是否为周视图
     *
     * @param isWeekView 是否为周视图
     */
    static void saveIsWeekView(boolean isWeekView) {
        Log.d("PreferencesDao", "saveIsWeekView: " + isWeekView);
        CourseTableApp.context.getSharedPreferences("data", Context.MODE_PRIVATE)
                .edit()
                .putBoolean("isWeekView", isWeekView)
                .apply();
    }

    /**
     * 保存考试安排记录数
     *
     * @param count 考试安排记录数
     */
    static void saveExamCount(int count) {
        CourseTableApp.context.getSharedPreferences("data", Context.MODE_PRIVATE)
                .edit()
                .putInt("examCount", count)
                .apply();
    }

    /**
     * 保存成绩记录数
     *
     * @param count 成绩记录数
     */
    static void saveScoreCount(int count) {
        CourseTableApp.context.getSharedPreferences("data", Context.MODE_PRIVATE)
                .edit()
                .putInt("scoreCount", count)
                .apply();
    }

    /**
     * 保存考试安排监控状态
     *
     * @param monitoring 是否已开启考试安排监控
     */
    static void saveExamMonitorState(boolean monitoring) {
        CourseTableApp.context.getSharedPreferences("data", Context.MODE_PRIVATE)
                .edit()
                .putBoolean("examMonitoring", monitoring)
                .apply();
    }

    /**
     * 保存成绩监控状态
     *
     * @param monitoring 是否已开启成绩监控
     */
    static void saveScoreMonitorState(boolean monitoring) {
        CourseTableApp.context.getSharedPreferences("data", Context.MODE_PRIVATE)
                .edit()
                .putBoolean("scoreMonitoring", monitoring)
                .apply();
    }

    /**
     * 保存乐器号
     *
     * @param midiNum 乐器号
     */
    static void saveMidiNum(int midiNum) {
        CourseTableApp.context.getSharedPreferences("data", Context.MODE_PRIVATE)
                .edit()
                .putInt("midiNum", midiNum)
                .apply();
    }

    /**
     * 获取用户名
     */
    static String getUsername() {
        return CourseTableApp.context.getSharedPreferences("data", Context.MODE_PRIVATE)
                .getString("username", null);
    }

    /**
     * 获取用户密码
     */
    static String getPassword() {
        String encryptedPassword = CourseTableApp.context.getSharedPreferences("data", Context.MODE_PRIVATE)
                .getString("password", null);
        if (encryptedPassword == null) {
            return null;
        }
        String password = AesUtils.decryptAES(encryptedPassword, AesUtils.AES_KEY);
        Log.d("PreferencesDao", "password: " + password);
        return password;
    }

    /**
     * 获取是否为周视图
     */
    static boolean isWeekView() {
        return CourseTableApp.context.getSharedPreferences("data", Context.MODE_PRIVATE)
                .getBoolean("isWeekView", true);
    }

    /**
     * 获取考试安排记录数
     *
     * @return 返回考试安排记录数
     */
    static int getExamCount() {
        return CourseTableApp.context.getSharedPreferences("data", Context.MODE_PRIVATE)
                .getInt("examCount", 0);
    }

    /**
     * 获取成绩记录数
     *
     * @return 返回成绩记录数
     */
    static int getScoreCount() {
        return CourseTableApp.context.getSharedPreferences("data", Context.MODE_PRIVATE)
                .getInt("scoreCount", 0);
    }

    /**
     * 获取考试安排监控状态
     *
     * @return 已开启返回 true，未开启返回 false
     */
    static boolean examMonitoring() {
        return CourseTableApp.context.getSharedPreferences("data", Context.MODE_PRIVATE)
                .getBoolean("examMonitoring", false);
    }

    /**
     * 获取成绩监控状态
     *
     * @return 已开启返回 true，未开启返回 false
     */
    static boolean scoreMonitoring() {
        return CourseTableApp.context.getSharedPreferences("data", Context.MODE_PRIVATE)
                .getBoolean("scoreMonitoring", false);
    }

    /**
     * 获取乐器号
     */
    static int getMidiNum() {
        return CourseTableApp.context.getSharedPreferences("data", Context.MODE_PRIVATE)
                .getInt("midiNum", 0);
    }
}
